'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const g = require('../nodes/identity.cjs'),
  l = require('./stringify.cjs'),
  o = require('./stringifyComment.cjs');
function y(t, f) {
  var h;
  const e = [];
  let m = f.directives === !0;
  if (f.directives !== !1 && t.directives) {
    const n = t.directives.toString(t);
    n ? (e.push(n), (m = !0)) : t.directives.docStart && (m = !0);
  }
  m && e.push('---');
  const c = l.createStringifyContext(t, f),
    { commentString: r } = c.options;
  if (t.commentBefore) {
    e.length !== 1 && e.unshift('');
    const n = r(t.commentBefore);
    e.unshift(o.indentComment(n, ''));
  }
  let u = !1,
    i = null;
  if (t.contents) {
    if (g.isNode(t.contents)) {
      if (
        (t.contents.spaceBefore && m && e.push(''), t.contents.commentBefore)
      ) {
        const p = r(t.contents.commentBefore);
        e.push(o.indentComment(p, ''));
      }
      (c.forceBlockIndent = !!t.comment), (i = t.contents.comment);
    }
    const n = i ? void 0 : () => (u = !0);
    let s = l.stringify(t.contents, c, () => (i = null), n);
    i && (s += o.lineComment(s, '', r(i))),
      (s[0] === '|' || s[0] === '>') && e[e.length - 1] === '---'
        ? (e[e.length - 1] = `--- ${s}`)
        : e.push(s);
  } else e.push(l.stringify(t.contents, c));
  if ((h = t.directives) != null && h.docEnd)
    if (t.comment) {
      const n = r(t.comment);
      n.includes(`
`)
        ? (e.push('...'), e.push(o.indentComment(n, '')))
        : e.push(`... ${n}`);
    } else e.push('...');
  else {
    let n = t.comment;
    n && u && (n = n.replace(/^\n+/, '')),
      n &&
        ((!u || i) && e[e.length - 1] !== '' && e.push(''),
        e.push(o.indentComment(r(n), '')));
  }
  return (
    e.join(`
`) +
    `
`
  );
}
exports.stringifyDocument = y;
